<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/5/21
 * Time: 6:36 AM
 */

namespace app\api\controller\v1;


use app\api\model\ProductOrder;
use app\api\model\ProductOrderContent;
use app\api\model\Store;
use app\api\model\StoreUser as StoreUserModel;
use app\api\model\TeamUser as TeamUserModel;
use app\api\model\Team as TeamModel;
use app\api\model\User as UserModel;
use app\api\model\UserInvitation;
use app\api\model\UserMoney as UserMoneyModel;
use app\api\model\UserMoney;

class Rank
{
    /**
     * 店铺排行
     */
    public function getStoreList($name, $start_time,$end_time)
    {
        $whereName = [];
        $where = [];
        $whereOne = [];
        $wherePhone = [];
        if($start_time) {
            $start_time = strtotime(date('Y-m-d',strtotime($start_time)).' 00:00:00');
            $end_time = strtotime(date('Y-m-d',strtotime($end_time)).' 23:59:59');
            $where[] = ['create_time','egt',$start_time];
            $where[] = ['create_time','elt',$end_time];
            $whereOne[] = ['pay_time','egt',$start_time];
            $whereOne[] = ['pay_time','elt',$end_time];
            $wherePhone[] = ['phone_time','egt',$start_time];
            $wherePhone[] = ['phone_time','elt',$end_time];
        }
        if($name) {
            $whereName[] = ['name', 'like', '%'.$name.'%'];
        }
        $stores = Store::where($whereName)->order('order_sort decs')->select()->toArray();
        foreach ($stores as $k=>$v){
//            $storeUsers = StoreUserModel::where('store_id',$v['id'])
//                ->with('user')->select()->toArray();
//            $store_user_ids = array_column($storeUsers,'id');
//            $whereTwo = $where;
//            $whereTwo[] = ['inviter','in',$store_user_ids];
//            $whereTwo[] = ['type','eq',2];
//            $userInvitation = UserInvitation::where($whereTwo)->select()->toArray();
//            $ids = array_column($userInvitation,'invitee');
            $whereThree = $whereOne;
            $whereThree[] = ['store_id','eq',$stores[$k]['id']];
            $whereThree[] = ['status','gt',1];
            $sale = ProductOrder::where($whereThree)->sum('actual_price');
            $shouXu = $sale*0.06;


            $user = UserModel::where('store_id','eq',$stores[$k]['id'])->select()->toArray();
            $user_ids = array_column($user,'id');
            $whereFour = $where;
            $whereFour[] = ['type','eq',1];
            $whereFour[] = ['user_id','in',$user_ids];
            $invite_money = UserMoney::where($whereFour)->sum('money');

            $userOne = UserModel::where('store_id','eq',$stores[$k]['id'])
                ->where('type','eq',1)->select()->toArray();
            $user_one_ids = array_column($userOne,'id');
            $whereNine = $where;
            $whereNine[] = ['type','eq',1];
            $whereTen[] = ['user_type','eq',1];
            $whereNine[] = ['user_id','in',$user_one_ids];
            $user_money = UserMoney::where($whereNine)->sum('money');

            $userTwo = UserModel::where('store_id','eq',$stores[$k]['id'])
                ->where('type','in',[2,3,4,8])->select()->toArray();
            $user_two_ids = array_column($userTwo,'id');
            $whereTen = $where;
            $whereTen[] = ['type','eq',1];
            $whereTen[] = ['user_type','eq',2];
            $whereTen[] = ['user_id','in',$user_two_ids];
            $store_money = UserMoney::where($whereTen)->sum('money');

            $whereFive = $whereOne;
            $whereFive[] = ['store_id','eq',$stores[$k]['id']];
            $whereFive[] = ['status','gt',1];
            $order = ProductOrder::where($whereFive)->select()->toArray();

            $order_ids = array_column($order,'id');
            $volume = ProductOrderContent::where('product_order_id','in',$order_ids)
                ->sum('product_number');

            // 裂变
            $whereEight = [];
            $whereEight[] = ['user.status','eq',3];
            $whereEight[] = ['user.type','eq',1];
            $whereEight[] = ['user.store_id', 'eq', $stores[$k]['id']];
            $user = UserModel::hasWhere('invite',function($query){
                $query->where('UserInvitation.type','eq',1);
            })->where($whereEight)->select()->toArray();
            $fission = 0;
            foreach ($user as $ka=>$va){
                $whereTwo = $whereOne;
                $whereTwo[] = ['user_id','eq',$user[$ka]['id']];
                $whereTwo[] = ['status', 'gt', 1];
                $order = ProductOrder::where($whereTwo)->find();
                if($order) {
                    $fission = $fission +1;
                }
            }
            $new_user = 0;
            $userInviteOne = UserInvitation::where('type',2)->select()->toArray();
            $idsOne = array_column($userInviteOne,'invitee');
            $whereFour = [];
            $whereFour[] = ['id','in',$idsOne];
            $whereFour[] = ['status','eq',3];
            $whereFour[] = ['new_type','eq',1];
            $whereFour[] = ['store_id','eq',$v['id']];
            $userOne = UserModel::where($whereFour)->select()->toArray();
            foreach ($userOne as $ka=>$va){
                $whereFive = $whereOne;
                $whereFive[] = ['user_id','eq',$userOne[$ka]['id']];
                $whereFive[] = ['status', 'gt', 1];
                $order = ProductOrder::where($whereFive)->find();
                if($order) {
                    $new_user += 1;
                }
            }
            $item =  [
                'sale' => $sale,
                'shou_xu' => round($shouXu,2),
                'volume' => $volume,
                'invite_money' => $invite_money,
                'user_money' => $user_money,
                'store_money' => $store_money,
                'new_user' => $new_user,
                'fission' => $fission,
            ];
            $stores[$k]['sale'] = $sale;
            $stores[$k]['rank'] = $item;
        }
        if(count($stores) > 0) {
            $stores = arraySort($stores,'sale');
        }
        return $stores;
    }


    /**
     * 店员排行
     */
    public function getStoreUserList($name,$store_id,$group_id,$type,$page,$limit,$start_time,$end_time)
    {
        $whereTen = [];
        $where = [];
        $whereOne = [];
        $wherePhone = [];
        if($name) {
            $whereTen[] = ['name','like','%'.$name.'%'];
        }
        if($store_id) {
            $whereTen[] = ['store_id', 'eq', $store_id];
        }
        if($group_id) {
            $whereTen[] = ['group_id', 'eq', $group_id];
        }
        if($type){
            $whereTen[] = ['type', 'eq', $type];
        }
        if($start_time) {
            $start_time = strtotime(date('Y-m-d',strtotime($start_time)).' 00:00:00');
            $end_time = strtotime(date('Y-m-d',strtotime($end_time)).' 23:59:59');
            $where[] = ['create_time','egt',$start_time];
            $where[] = ['create_time','elt',$end_time];
            $whereOne[] = ['pay_time','egt',$start_time];
            $whereOne[] = ['pay_time','elt',$end_time];
            $wherePhone[] = ['phone_time','egt',$start_time];
            $wherePhone[] = ['phone_time','elt',$end_time];
        }
        $result = StoreUserModel::where($whereTen)->with('store,user,group')->order('id desc')->select()->each(function ($item,$k) use($where,$whereOne,$wherePhone){
            if($item['type'] == 1) {
                $item['type_name'] = '店长';
            } elseif($item['type'] == 2) {
                $item['type_name'] = '组长';
            } elseif($item['type'] == 3) {
                $item['type_name'] = '店员';
            }
            $whereTwo[] = ['inviter','eq',$item['id']];
            $whereTwo[] = ['type','eq',2];
            $userInvitation = UserInvitation::where($whereTwo)->select()->toArray();
            $ids = array_column($userInvitation,'invitee');
            $whereThree = $whereOne;
            $whereThree[] = ['user_id','in',$ids];
            $whereThree[] = ['status','gt',1];
            $sale = ProductOrder::where($whereThree)
                ->sum('actual_price');
            $whereSelf[] = ['user_id','eq',$item['user_id']];
            $whereSelf[] = ['status','gt',1];
            $sale_one = ProductOrder::where($whereSelf)
                ->sum('actual_price');
            $sale = $sale + $sale_one;
            $whereFour = $where;
            $whereFour[] = ['user_id','eq',$item['user_id']];
            $whereFour[] = ['type','eq',1];
            $invite_money = UserMoneyModel::where($whereFour)
                ->sum('money');
            $whereSix = $wherePhone;
            $whereSix[] = ['store_id','eq',$item['store_id']];
            $whereSix[] = ['id','in',$ids];
            $whereSix[] = ['status','eq',3];
            $whereSix[] = ['new_type','eq',1];
            $new_user = UserModel::where($whereSix)->count();
            $items =  [
                'sale' => $sale,
                'invite_money' => $invite_money,
                'new_user' => $new_user,
            ];
            $item['rank'] = $items;
            $item['sale'] = $sale;
            $item['order_sort'] = $k+1;

        })->toArray();
        if(count($result) > 0) {
            $result = arraySort($result,'sale');
        }
        $data['count'] = count($result);
        $data['data'] = $result;
        return $data;
    }

    public function getTeamList($name,$page,$limit,$start_time,$end_time)
    {
        $whereTen = [];
        $where = [];
        $whereOne = [];
        $wherePhone = [];
        if($name) {
            $whereTen[] = ['name','like','%'.$name.'%'];
        }
        if($start_time) {
            $where[] = ['create_time','egt',$start_time];
            $where[] = ['create_time','elt',$end_time];
            $whereOne[] = ['pay_time','egt',$start_time];
            $whereOne[] = ['pay_time','elt',$end_time];
            $wherePhone[] = ['phone_time','egt',$start_time];
            $wherePhone[] = ['phone_time','elt',$end_time];
        }
        $query = TeamModel::where($whereTen)->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item) use($where,$whereOne,$wherePhone){
            $teamUsers = TeamUserModel::where('team_id',$item['id'])->with('user')->select()->toArray();
            $team_user_ids = array_column($teamUsers,'id');
            $whereTwo = $where;
            $whereTwo[] = ['inviter','in',$team_user_ids];
            $whereTwo[] = ['type','eq',3];
            $userInvitation = UserInvitation::where($whereTwo)->select()->toArray();
            $ids = array_column($userInvitation,'invitee');
            $whereThree = $whereOne;
            $whereThree[] = ['user_id','in',$ids];
            $whereThree[] = ['status','gt',1];
            $sale = ProductOrder::where($whereThree)
                ->sum('actual_price');
            $whereSelf = $whereOne;
            $user_ids = array_column($teamUsers,'user_id');
            $whereSelf[] = ['user_id','in',$user_ids];
            $whereSelf[] = ['status','gt',1];
            $sale_one = ProductOrder::where($whereSelf)
                ->sum('actual_price');
            $sale = $sale + $sale_one;
            $whereFour = $where;
            $whereFour[] = ['user_id','in',$ids];
            $whereFour[] = ['type','eq',1];
            $invite_money = UserMoneyModel::where($whereFour)
                ->sum('money');
            $whereSix = $wherePhone;
            $whereSix[] = ['team_id','eq',$item['id']];
            $whereSix[] = ['status','eq',3];
            $new_user = UserModel::where($whereSix)
                ->count();
            $fission = 0;
            $whereSeven = $wherePhone;
            $whereSeven[] = ['id','in',$ids];
            $whereSeven[] = ['status','eq',3];
            $number = UserModel::where($whereSeven)->count();
            if($number > 0){
                $fission += $number;
            }
            $items =  [
                'sale' => $sale,
                'invite_money' => $invite_money,
                'new_user' => $new_user,
                'fission' => $fission,
            ];
            $item['rank'] = $items;
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    public function getTeamUserList($name,$team_id,$type,$page,$limit,$start_time,$end_time)
    {
        $whereTen = [];
        if($name) {
            $whereTen[] = ['name','like','%'.$name.'%'];
        }
        if($team_id) {
            $whereTen[] = ['team_id', 'eq', $team_id];
        }
        if($type) {
            $whereTen[] = ['type','eq',$type];
        }
        $where = [];
        $whereOne = [];
        $wherePhone = [];
        if($start_time) {
            $where[] = ['create_time','egt',$start_time];
            $where[] = ['create_time','elt',$end_time];
            $whereOne[] = ['pay_time','egt',$start_time];
            $whereOne[] = ['pay_time','elt',$end_time];
            $wherePhone[] = ['phone_time','egt',$start_time];
            $wherePhone[] = ['phone_time','elt',$end_time];
        }
        $query = TeamUserModel::where($whereTen)->with('team,user')->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item) use($where,$whereOne,$wherePhone){
            if($item['type'] == 1) {
                $item['type_name'] = '团长';
            } elseif($item['type'] == 2) {
                $item['type_name'] = '团员';
            }
            $whereTwo = $where;
            $whereTwo[] = ['inviter','eq',$item['id']];
            $whereTwo[] = ['type','eq',3];
            $userInvitation = UserInvitation::where($whereTwo)->select()->toArray();
            $ids = array_column($userInvitation,'invitee');
            $whereThree = $whereOne;
            $whereThree[] = ['user_id','in',$ids];
            $whereThree[] = ['status','gt',1];
            $sale = ProductOrder::where($whereThree)
                ->sum('actual_price');
            $whereSelf = $whereOne;
            $whereSelf[] = ['user_id','in',$item['user_id']];
            $whereSelf[] = ['status','gt',1];
            $sale_one = ProductOrder::where($whereSelf)
                ->sum('actual_price');
            $sale = $sale + $sale_one;
            $whereFour = $where;
            $whereFour[] = ['user_id','eq',$item['user_id']];
            $whereFour[] = ['type','eq',1];
            $invite_money = UserMoneyModel::where($whereFour)
                ->sum('money');
            $whereSix = $wherePhone;
            $whereSix[] = ['team_id','eq',$item['id']];
            $whereSix[] = ['status','eq',3];
            $new_user = UserModel::where($whereSix)
                ->count();
            $fission = 0;
            $whereSeven = $wherePhone;
            $whereSeven[] = ['id','in',$ids];
            $whereSeven[] = ['status','eq',3];
            $number = UserModel::where($whereSeven)->count();
            if($number > 0){
                $fission += $number;
            }
            $items =  [
                'sale' => $sale,
                'invite_money' => $invite_money,
                'new_user' => $new_user,
                'fission' => $fission,
            ];
            $item['rank'] = $items;
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }
}